use sea_orm_migration::prelude::*;

#[derive(DeriveMigrationName)]
pub struct Migration;

#[async_trait::async_trait]
impl MigrationTrait for Migration {
    async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .create_table(
                Table::create()
                    .table(Addresses::Table)
                    .if_not_exists()
                    .col(
                        ColumnDef::new(Addresses::Id)
                            .string()
                            .not_null()
                            .primary_key(),
                    )
                    .col(ColumnDef::new(Addresses::CustomerId).string().not_null())
                    .col(ColumnDef::new(Addresses::GuestId).string().not_null())
                    .col(ColumnDef::new(Addresses::Name).string().not_null())
                    .col(ColumnDef::new(Addresses::Email).string())
                    .col(ColumnDef::new(Addresses::Phone).string().not_null())
                    .col(ColumnDef::new(Addresses::CountryId).string().not_null())
                    .col(ColumnDef::new(Addresses::StateId).string().not_null())
                    .col(ColumnDef::new(Addresses::State).string().not_null())
                    .col(ColumnDef::new(Addresses::CityId).string())
                    .col(ColumnDef::new(Addresses::City).string().not_null())
                    .col(ColumnDef::new(Addresses::Zipcode).string().not_null())
                    .col(ColumnDef::new(Addresses::Address1).string().not_null())
                    .col(ColumnDef::new(Addresses::Address2).string().not_null())
                    .col(
                        ColumnDef::new(Addresses::Default)
                            .boolean()
                            .not_null()
                            .default(false),
                    )
                    .col(
                        ColumnDef::new(Addresses::CreatedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .col(
                        ColumnDef::new(Addresses::UpdatedAt)
                            .timestamp_with_time_zone()
                            .null(),
                    )
                    .to_owned(),
            )
            .await
    }

    async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
        manager
            .drop_table(Table::drop().table(Addresses::Table).to_owned())
            .await
    }
}

#[derive(DeriveIden)]
enum Addresses {
    Table,
    Id,
    CustomerId,
    GuestId,
    Name,
    Email,
    Phone,
    CountryId,
    StateId,
    State,
    CityId,
    City,
    Zipcode,
    Address1,
    Address2,
    Default,
    CreatedAt,
    UpdatedAt,
}
